window.onload = () => {
    document.querySelector(".bookR .bookinfo03 span").onclick = () => {
        let showMore = document.querySelector(".bookR .bookinfo03 p");
        if (showMore.className === "show-more") {
            showMore.className = "";
            document.querySelector(".bookR .bookinfo03 span").innerText = "展开";
        }
        else {
            showMore.className += "show-more";
            document.querySelector(".bookR .bookinfo03 span").innerText = "收起";
        }
    };
    
    let btns = document.querySelectorAll(".comment-item .comment-item-body span");
    let showMore = document.querySelectorAll(".comment-item .comment-item-body p");

    for (let i = 0; i < btns.length; i++) {
        btns[i].onclick = () => {
            // console.log(i);
            if (showMore[i].className === "show-more") {
                showMore[i].className = "";
                btns[i].innerHTML = "展开";
            } else {
                showMore[i].className = "show-more";
                btns[i].innerHTML = "收起";
            }
        }
    }
    var bid = window.location.href.split("/")[window.location.href.split("/").length - 1];
    bid = parseInt(bid);
    // console.log(bid);
    $.ajax({
        type: "GET",
        url: `/bookcity/book/detail/${bid}`,

        success: function (data) {
            data = JSON.parse(data);
            // console.log(data);
            // 图书信息
            $(".bookname h2").text(data.book.bname);
            $(".bookinfo01 p .author").text("作者：" + data.book.author);
            $(".bookinfo01 p span:nth-child(2)").text("出版社：" + data.book.press);
            $(".bookinfo01 p span:nth-child(3)").text("出版时间：" + data.book.pubdate);
            $(".bookinfo02 p").text(`价格：${data.book.price}￥`);
            $(".bookinfo03 p").text(data.book.brief);
            // 封面
            // $("bookL img").src
            // 评论
            $(".comment-head span:nth-child(3)").text(data.comment.count + "条");
            let items = $(".comment-item");
            let uportrait = $(".comment-item-head img");
            let uname = $(".comment-item-head p:nth-child(1)");
            let date = $(".comment-item-head p:nth-child(2)");
            let comment = $(".comment-item-body p");

            // 复原数据
            for(let i = 0; i < items.length; i++) {
                items[i].hidden = false;
                // uportrait[i].hidden = false;
            }
            let len = function(dlen, clen) {
                if(dlen < clen) {
                    // for(let i = dlen; i < ilen; i++) {
                    //     items[i].hidden = true;
                    // }
                    return dlen;
                } else 
                    return clen;
            }(data.comment.count, items.length);

            data = data.comment.data;
            for(let i = 0; i < len; i++) {
                uportrait[i].src = data[i].portrait;
                uname[i].innerText = data[i].uname;
                date[i].innerText = data[i].date;
                comment[i].innerText = data[i].content;
            }
        }
    })

    // 购买
    $(".buy").on("click", () => {
        $.ajax({
            type: "POST",
            url: "/bookcity/book/buy",
            data: {
                "uid": $.cookie("uid"),
                "bid": bid,
                "time": Math.floor(new Date().getTime() / 1000),
                "num": 1,
                "price": $(".bookinfo02 p").text().split("￥")[0].split("：")[1]
            },
            success: function (data) {
                data = JSON.parse(data);
                if(data.state > 0) {
                    alert("购买成功");
                } else {
                    alert("购买失败");
                }
            }
        })
    })

    $(".addcommentIco").on("click", function () {
            if($("#youComment").css("display") === "none") {
                $("#youComment").css("display", "block");
            } else {
                $("#youComment").css("display", "none");
            }
        }
    )

    $("#youComment > form > button").on("click", function () {
            let content = $("#youComment > form > textarea").val();
            let uid = $.cookie("uid");
            $.ajax({
                type: "POST",
                url: "/bookcity/userSL/addComment",
                data: {
                    "uid": uid,
                    "bid": bid,
                    "content": content,
                    "time": Math.floor(new Date().getTime() / 1000)
                },
                success: function (data) {
                    data = JSON.parse(data);
                    if(data.state > 0) {
                        alert("评论成功");
                    } else {
                        alert("评论失败");
                    }
                }
            })
        }
    )
};

function addComment(){
    document.querySelector(".comment .comment-head .headcontainer .addcomment .youComment").style.display = "block";
}